package com.jerei.web.utils.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import java.util.Locale;

/**
 * Created by sunfeihu on 2016/11/11.
 */
public class CommonWebRequestInterceptor implements WebRequestInterceptor {

    @Autowired
    private CookieLocaleResolver cookieLocaleResolver;

    @Override
    public void preHandle(WebRequest request) throws Exception {
        Locale locale = request.getLocale();
        String lang = "zh";
        if (null != locale) {
            lang = locale.getLanguage();
        }
        request.setAttribute("lang", lang, WebRequest.SCOPE_REQUEST);
    }

    @Override
    public void postHandle(WebRequest request, ModelMap model) throws Exception {

    }

    @Override
    public void afterCompletion(WebRequest request, Exception ex) throws Exception {

    }
}
